package com.bbzn.device.client.service.emq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.config.Constants;
import com.bbzn.device.client.dataobject.Strategy;
import com.bbzn.device.client.service.StrategyService;
import com.bbzn.device.client.service.emq.smoke.SmokeStrategy;
import com.bbzn.device.client.utils.SpringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

/**
 * @Author wulongbo
 * @Date 2021/7/3 15:28
 * @Version 1.0
 */

@Service
@Slf4j
public class SubscribeNbService {

    @Autowired
    private StrategyService strategyService;

    @Bean
    @ServiceActivator(inputChannel = Constants.MQTT_NB_SUBSCRIBE_CHANNEL)
    public MessageHandler nbMessageHandler() {
        MessageHandler messageHandler = new MessageHandler() {
            @SneakyThrows
            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                log.info("NB-烟感订阅者订阅消息头是:" + message.getHeaders());
                log.info("NB订阅者订阅主体是:" + message.getPayload());
                // 区分主题订阅模式，方便不再需要判断topic
//                String topic = message.getHeaders().get("mqtt_receivedTopic").toString();
//                String type = topic.substring(topic.lastIndexOf("/")+1, topic.length());
//                if("topic1".equalsIgnoreCase(topic)){
//                    System.out.println("topic1,"+message.getPayload().toString());
//                }else if("topic2".equalsIgnoreCase(topic)){
//                    System.out.println("topic2,"+message.getPayload().toString());
//                }
                //执行业务
                nbHandle((String) message.getPayload());
            }
        };
        return messageHandler;
    }


    public String nbHandle(String payLoad) {
        try {
            JSONObject jsonObject = JSON.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            String channelCode = jsonObject.getString("code");
            //业务判断
            if (null == channelCode) {
                return "channerlCode不能为空";
            }
            // 2.根据code查询具体业务实现
            Strategy strategy = strategyService.findOneByChannelCode(Long.valueOf(channelCode));
            // 2.根据code查询具体业务实现

            if (null == strategy) {
                return "没有查询到该渠道信息";
            }
            String strategyBeanId = strategy.getStrategyBeanId();
            if (null == strategyBeanId) {
                return "没有配置策略BeanId";
            }
            // 使用beanid从容器获取对象
            SmokeStrategy smokeStrategy = SpringUtils.getBean(strategyBeanId, SmokeStrategy.class);
            // 执行业务
            smokeStrategy.dealAlarm(channelCode, imei);
        } catch (Exception e) {
            e.printStackTrace();
            return "处理失败 : 参数信息" + e.getMessage();
        }
        return "";
    }
}
